package com.zsqat.eshop.seckill.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class SeckillController {
  @Autowired
  private SeckillService seckillService;

  /**
   * 当前时间可以参与秒杀的商品信息
   * @return
   */
  @GetMapping(value = "/getCurrentSeckillSkus")
  @ResponseBody
  public R getCurrentSeckillSkus() {

    //获取到当前可以参加秒杀商品的信息
    List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();

    return R.ok().setData(vos);
  }


  /**
   * 根据skuId查询商品是否参加秒杀活动
   * @param skuId
   * @return
   */
  @GetMapping(value = "/sku/seckill/{skuId}")
  @ResponseBody
  public R getSkuSeckilInfo(@PathVariable("skuId") Long skuId) {

    SeckillSkuRedisTo to = seckillService.getSkuSeckilInfo(skuId);

    return R.ok().setData(to);
  }


  /**
   * 商品进行秒杀(秒杀开始)
   * @param killId
   * @param key
   * @param num
   * @return
   */
  @GetMapping(value = "/kill")
  public String seckill(@RequestParam("killId") String killId,
                        @RequestParam("key") String key,
                        @RequestParam("num") Integer num,
                        Model model) {

    String orderSn = null;
    try {
      //1、判断是否登录
      orderSn = seckillService.kill(killId,key,num);
      model.addAttribute("orderSn",orderSn);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "success";
  }
}
